﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Zhongy
{
    public class MananerBase : MonoBase
    {
        private Dictionary<int, List<MonoBase>> dict = new Dictionary<int, List<MonoBase>>();

        public virtual void Execute(int evtCode,params object[] message)
        {
            if (!dict.ContainsKey(evtCode))
            {
                Debug.Log(string.Format("未有注册过此类型消息{0}", evtCode));
                return;
            }

            List<MonoBase> itemLst = dict[evtCode];
            if (itemLst != null && itemLst.Count > 0)
            {
                foreach (var item in itemLst)
                {
                    item.Execute(evtCode, message);
                }
            }
        }

        public void Bind(int evtCode, MonoBase mono)
        {
            if (!dict.ContainsKey(evtCode) || dict[evtCode] == null)
            {
                dict[evtCode] = new List<MonoBase>();
            }

            List<MonoBase> itemLst = dict[evtCode];

            if (!itemLst.Contains(mono))
                itemLst.Add(mono);
        }

        public void Bind(int[] evtCodes, MonoBase mono)
        {
            for (int i = 0; i < evtCodes.Length; i++)
            {
                Bind(evtCodes[i], mono);
            }
        }

        public void UnBind(int evtCode, MonoBase mono)
        {
            if (!dict.ContainsKey(evtCode))
            {
                Debug.Log(string.Format("未有注册过此类型消息{0}", evtCode));
                return;
            }

            if (dict[evtCode] == null )
            {
                dict.Remove(evtCode);
                return;
            }

            List<MonoBase> itemLst = dict[evtCode];

            if (itemLst.Contains(mono))
                itemLst.Remove(mono);

            if (itemLst.Count == 0)
                dict.Remove(evtCode);
        }

        public void UnBind(int[] evtCodes, MonoBase mono)
        {
            for (int i = 0; i < evtCodes.Length; i++)
            {
                UnBind(evtCodes[i], mono);
            }
        }
    }
}
